//
//  LBCategoryFoodCell.m
//  loveBeen
//
//  Created by 李田 on 16/11/20.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBCategoryFoodCell.h"
#import "LBGoodsModel.h"
#import "LBCategoryFoodCell.h"
#import "LBActRowsModel.h"
#import "LBCategoryDetailModel.h"
#import "LBSuperMarketProducts.h"
#import "LBPlusMinusTool.h"
#import "LBNotEnouchView.h"
@interface LBCategoryFoodCell ()

//图片
@property(nonatomic, weak)UIImageView *imgV;
//名称
@property(nonatomic, weak)UILabel *nameL;
//价格
@property(nonatomic, weak)UILabel *priceL;
//重量
@property(nonatomic, weak)UILabel *specificsL;
@end

@implementation LBCategoryFoodCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
//    MARK:

- (void)setGoodModel:(LBGoodsModel *)goodModel{
    
    _goodModel = goodModel;
    self.nameL.text = goodModel.name;
    self.priceL.text = [NSString stringWithFormat:@"¥%@",goodModel.price.description];
    self.specificsL.text = goodModel.specifics;
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:goodModel.img]];
}


#pragma mark - 搭建界面
- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    //    MARK: icon图片
    UIImageView *iconimageV = [[UIImageView alloc] init];
    iconimageV.image = [UIImage imageNamed:@"meinv_4"];
    [self.contentView addSubview:iconimageV];
    //    MARK: title标题
    UILabel *titleL = [UILabel cz_labelWithText:@"爱鲜蜂-15度蜜梨" fontSize:13 color:[UIColor blackColor]];
    [self.contentView addSubview:titleL];
    //    MARK: 精选图片
    UIImageView *choicenessImageV = [[UIImageView alloc] init];
    choicenessImageV.image = [UIImage imageNamed:@"jingxuan.png"];
    [self.contentView addSubview:choicenessImageV];
    //    MARK: 买一赠一
    UIImageView *preImageV = [[UIImageView alloc] init];
    preImageV.image = [UIImage imageNamed:@"buyOne.png"];
    [self.contentView addSubview:preImageV];
    //    MARK: 重量
    UILabel *weightL = [UILabel cz_labelWithText:@"500g/盒" fontSize:12 color:[UIColor cz_colorWithHex:0x878787]];
    [self.contentView addSubview:weightL];
    //    MARK: 价格
    UILabel *priceL = [UILabel cz_labelWithText:@"¥12.9" fontSize:14 color:[UIColor cz_colorWithHex:0xFC2B07]];
    [self.contentView addSubview:priceL];
    //    MARK: 加号按钮
    UIButton *addBtn =[[UIButton alloc] init];
    [addBtn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"v2_increased"] forState:UIControlStateSelected];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:addBtn];
    //    MARK: 关联
    self.imgV = iconimageV;
    self.nameL = titleL;
    self.priceL = priceL;
    self.specificsL = weightL;
    
    //    MARK: 布局
    [iconimageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(125, 122));
    }];
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(7);
        make.top.equalTo(iconimageV.mas_bottom);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(20);
    }];
    
    [choicenessImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL);
        make.top.equalTo(titleL.mas_bottom).offset(4);
        make.size.mas_equalTo(CGSizeMake(22, 13));
    }];
    
    [preImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(choicenessImageV.mas_right).offset(4);
        make.top.equalTo(choicenessImageV);
        make.size.mas_equalTo(CGSizeMake(50, 13));
    }];
    
    [weightL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL);
        make.top.equalTo(choicenessImageV.mas_bottom).offset(8);
    }];
    
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weightL);
        make.top.equalTo(weightL.mas_bottom);
    }];
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-6);
        make.bottom.mas_equalTo(-4);
    }];
}

#pragma mark - addBtn的点击事件
- (void)addBtnClick: (UIButton *)addBtn{
    
   __block  LBSuperMarketProducts *superModel = [[LBSuperMarketProducts alloc] init];
    
    superModel.name = self.goodModel.name;
    superModel.partner_price = self.goodModel.price.description;
    superModel.img = self.goodModel.img;
    superModel.totalCount++;
    superModel.selected = YES;
    LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];
    __block BOOL equel = NO;
    [manager.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![model.name isEqualToString:superModel.name]) {
            equel = NO;
        }else{
            equel = YES;
            superModel = model;
        }
    }];
    
    if (!equel) {
        [manager.selectedProductsModel addObject:superModel];
    }
    else{
        superModel.totalCount++;
    }
    
    manager.change = YES;
//    [[LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel addObject:superModel];
    
    //    MARK: 坐标转换
    //1.0 获取主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    //2.0转换
    
    self.startP = [self convertPoint:self.imgV.center toView:keyWindow];
    if ([_delegate respondsToSelector:@selector(categoryFoodCell:startPoint:urlStr:)]) {
        [_delegate categoryFoodCell:self startPoint:self.startP urlStr: _goodModel.img];
    }
    
    //    MARK: 添加提示框
    if (superModel.totalCount == 5) {
        
        LBNotEnouchView *notTouchV = [[LBNotEnouchView alloc] initWithName:self.goodModel.name];
        
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:notTouchV];
        
        [notTouchV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(230);
            make.left.mas_equalTo(80);
            make.right.mas_equalTo(-80);
            make.height.mas_equalTo(150);
        }];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [notTouchV removeFromSuperview];
        });
    }
}

@end
